<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth extends MY_Controller {
	function __construct(){
   		parent::__construct();
  		$this->load->model('user');
   	}
	function index(){
		redirect('auth/login', 'refresh');
	}
	//Login Time!
	function login(){
		$this->title = "Login";
		$this->load->helper(array('form'));
                $this->load->model('user');
	    //check session
	    if($this->session->userdata('logged_in')){
			redirect('home', 'refresh');
	    }else{
			$this->load->library('form_validation');
                        $this->form_validation->set_error_delimiters('<p class=text-error>', '</p>'); 
			$this->form_validation->set_rules('inputUsername', 'Username', 'trim|required|xss_clean');
		   	$this->form_validation->set_rules('inputPassword', 'Password', 'trim|required|xss_clean|callback_check_login');
		   	if($this->form_validation->run() == FALSE){
		     	//Field validation failed.&nbsp; User redirected to login page 
				$this->_render('auth/login');
				//$this->load->view('auth/login');
		  	}else{
                                $get_result = $this->user->login();
                                if ($get_result)
                                {
                                    redirect('home', 'refresh');
                                    //return TRUE;
                                }
                                else
                                {
                                    $this->form_validation->set_message('login', 'Invalid username or password');
                                    return FALSE;
                                }
		  	}
	    }
	    
	}
	function check_login(){
            $result = $this->user->login();
            if($result){
     		$sess_array = array();
     		foreach($result as $row){
       			$sess_array = array(
         		'cust_id' => $row->cust_id,
         		'username' => $row->username
       			);
       			$this->session->set_userdata('cus_login', $sess_array);
     		}
     		return TRUE;
            }else{
     		$this->form_validation->set_message('check_login', 'Invalid username or password');
     		return FALSE;
            }
	}
	function logout(){
        $this->session->unset_userdata('cus_login');
        redirect('home', 'refresh');
   	}
   	//End Login

   	//Register Time!
   	function register(){
   		$this->title = "Register";
                $this->load->helper('form');
                $this->load->library('form_validation');
                $this->load->model('user');
                
                $this->form_validation->set_error_delimiters('<p class=text-error>', '</p>');  
                $this->form_validation->set_rules('Username', 'Username', 'trim|required|xss_clean|min_length[4]|callback_username_check');
                $this->form_validation->set_rules('Password', 'Password', 'trim|required|xss_clean|min_length[4]');
                $this->form_validation->set_rules('conPassword', 'conPassword', 'trim|required|xss_clean|min_length[4]|matches[Password]');
                $this->form_validation->set_rules('Email', 'Email', 'trim|required|xss_clean|valid_email');
                $this->form_validation->set_rules('conEmail', 'Email', 'trim|required|xss_clean|valid_email|matches[Email]');
                $this->form_validation->set_rules('Firstname', 'Firstname', 'trim|required|xss_clean');
                $this->form_validation->set_rules('Lastname', 'Lastname', 'trim|required|xss_clean');
                $this->form_validation->set_rules('sex', 'sex', 'trim|required|xss_clean|callback_sex_check');
                $this->form_validation->set_rules('birthdate', 'date', 'trim|required|xss_clean');
                $this->form_validation->set_rules('iden', 'iden', 'trim|required|xss_clean|numeric');
                $this->form_validation->set_rules('tel', 'tel', 'trim|required|xss_clean|numeric');
                $this->form_validation->set_rules('address', 'address', 'trim|required|xss_clean');
                $this->form_validation->set_rules('prov', 'prov', 'trim|required|xss_clean|callback_prov_check');
                $this->form_validation->set_rules('code', 'code', 'trim|required|xss_clean|numeric');
        
                $this->form_validation->set_rules('agree[]', 'agree', 'trim|required|xss_clean');
                
                
                if($this->form_validation->run() == FALSE)
                {
		     	//Field validation failed.&nbsp; User redirected to login page 
                    
                    $this->_render('auth/register');
		}
                else
                {
                    //redirect('home', 'refresh');
                    $this->user->register(); 
                    $this->data['registed'] = TRUE;
                    $this->_render('auth/register');
                }
   	}
        function username_check()
	{
            $this->load->model('user');
                $get_result = $this->user->check_username_availablity();
		if ($get_result)
		{
                    $this->form_validation->set_message('username_check', 'This %s is not available');
                    return FALSE;
		}
		else
		{
                    return TRUE;
		}
	}
        function sex_check(){
		if ($this->input->post('sex') == "-1")
		{
                    $this->form_validation->set_message('sex_check', 'Please select your sex.');
                    return FALSE;
		}
		else
		{
                    return TRUE;
		}
        }
        function prov_check(){
		if ($this->input->post('prov') == "จังหวัด")
		{
                    $this->form_validation->set_message('prov_check', 'Please select your Province.');
                    return FALSE;
		}
		else
		{
                    return TRUE;
		}
        }
}